package com.xiaochuan.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;



@Configuration//声明配置类
@EnableOpenApi//开启swagger注
public class SwaggerConfig  {
    @Bean
    public Docket createRestApi(Environment environment){
        Profiles of = Profiles.of("dev","test");
        boolean b = environment.acceptsProfiles(of);//获取当前的开发环境
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(b)//为false 则不能启动swagger
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xiaochuan.controller"))//指定controller层
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                //页面标题
                .title("鋆石后台接口文档")
                //创建人
                .contact(new Contact("dy","www.boatapi.com/cloudboatpm",""))
                //版本号
                .version("1.0")
                //描述
                .description("后台api集合")
                .build();
    }




    //如果项目中集成了shiro那就要在shiro的配置类中设置允许swagger资源通过
    // swagger放行路径
    // filterMap.put("/swagger‐ui.html", "anon");
    // filterMap.put("/swagger‐resources/**", "anon");
    // filterMap.put("/v2/**", "anon");
    // filterMap.put("/webjars/**", "anon");
}
